package com.tgy.leetcode;

import java.util.Arrays;

/**
 * @Author: tgy
 * @Date: 12/31/20 9:15 AM
 *
 *  最早结束的才能参加更多的区间
 *
 *    ---- --
 *      -------
 *   ----------
 *
 *   https://leetcode-cn.com/problems/non-overlapping-intervals/
 */
public class _435_无重叠区间 {

    public int eraseOverlapIntervals(int[][] intervals) {

        if (intervals == null || intervals.length <= 1) {

            return 0;
        }

        Arrays.sort(intervals,(o1, o2) -> {

            return o1[1] - o2[1];
        });

        int count = 1;
        int endTime = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {

            if (intervals[i][0] >= endTime) {

                count++;
                endTime = intervals[i][1];
            }

        }

        return intervals.length - count;
    }

}
